# Copyright (c) 2021-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.5.2 FATAL_ERROR)

project(plugin_ecmascript)
message(STATUS "ecmascript plugin is found")

add_definitions(-DENABLE_BYTECODE_OPT)
add_definitions(-DARK_INTRINSIC_SET)
panda_target_compile_definitions(arkcompiler PUBLIC -DENABLE_BYTECODE_OPT -DARK_INTRINSIC_SET)
panda_target_compile_definitions(arkbytecodeopt PUBLIC -DENABLE_BYTECODE_OPT -DARK_INTRINSIC_SET)

add_custom_target(ecmascript_tests COMMENT "Running ecmascript test suites")
set(DEFAULT_TEST_GROUP ecmascript_tests)

if(TARGET es2panda_tests)
    add_dependencies(ecmascript_tests es2panda_tests)
endif()

# NB! ADDING THIS PROPERTY IS ALLOWED ONLY IN SPECIAL CASES. DO NOT COPY-PASTE IT.
set_target_properties(ecmascript_tests PROPERTIES first-level-tests-dependency TRUE)
add_dependencies(tests ecmascript_tests)

# List for accumulation of all ecmascript gtests binary paths.
# It's used by CI to archive these binaries into a single artifact
# and send it to second stage where unit tests will use them.
set_property(GLOBAL PROPERTY ecmascript_stash_list "")

if(PANDA_WITH_TOOLCHAIN)
    add_subdirectory(assembler)
    add_subdirectory(isa)
    add_subdirectory(irtoc_scripts)
endif()

add_subdirectory(bytecode_optimizer)

if(PANDA_WITH_COMPILER)
    add_subdirectory(compiler)
endif()

if(PANDA_WITH_RUNTIME)
    add_subdirectory(ecmastdlib)
    add_subdirectory(runtime)
endif()

if(PANDA_WITH_TESTS)
    add_subdirectory(tests)
endif()

add_plugin_options(${CMAKE_CURRENT_SOURCE_DIR}/ecmascript_plugin_options.yaml)

add_runtime_options(${CMAKE_CURRENT_SOURCE_DIR}/runtime_options.yaml)

add_entrypoints_yaml(${CMAKE_CURRENT_SOURCE_DIR}/runtime/ecma_entrypoints.yaml)

# ----- Collecting ecmascript gtests paths for CI stash ---------------------------------
# NB! This must be the last section!

# Write to a file the list of all binaries
get_property(stash_files GLOBAL PROPERTY ecmascript_stash_list)
list(JOIN stash_files "\n" file_content)
file(WRITE ${PANDA_BINARY_ROOT}/ecmascript_stash_files.txt "${file_content}\n")
